<template>
  <div>
    <a-form :form="form" class="form">
      <a-card :bordered="false" :title=" pid > 0?'社区信息':'街道信息'">
        <a-row class="form-row">
          <a-col :lg="{ span: 7 }" :sm="24">
            <a-form-item :label="pid > 0?'社区名称':'街道名称'">
              <a-input
                type="textarea"
                placeholder="请输入街道/社区名称"
                v-decorator="[
                  'name',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入街道/社区名称',
                      },
                      { max: 255, message: '不得超过255个字符' },
                    ],
                    initialValue:formData.name
                  },
                ]"
              />
            </a-form-item>
          </a-col>

          <a-col :lg="{ span: 7, offset: 1 }" :sm="24">
            <a-form-item label="排序(纯数字,越大越往前显示)">
              <a-input
                placeholder="请输入排序"
                v-decorator="[
                  'sort',
                  {
                    rules: [{ validator: checkNumber }],
                    initialValue: formData.sort,
                  },
                ]"
              />
            </a-form-item>
          </a-col>

          <a-col :lg="{ span: 7, offset: 1 }" :sm="24" >
            <a-form-item label="区划代码" v-if="pid == 0">
              <a-input
                type="textarea"
                placeholder="请输入区划代码"
                v-decorator="[
                  'zoningCode',
                  {
                    rules: [
                      {
                        message: '请输入区划代码',
                      },
                      { max: 255, message: '不得超过255个字符' },
                    ],
                     initialValue: formData.zoningCode,
                  },
                ]"
              />
            </a-form-item>

             <a-form-item label="状态" v-else>
               <a-switch
                checked-children="启用"
                un-checked-children="禁用"
                v-decorator="[
                  'status',
                  { valuePropName: 'checked', initialValue: formData.status == 1?true:false },
                ]"
              />
            </a-form-item>
          </a-col>



          
        </a-row>

        <a-row class="form-row" v-if="pid == 0">
          <a-col :lg="{ span: 7 }" :sm="24">
            <a-form-item label="状态">
              <a-switch
                checked-children="启用"
                un-checked-children="禁用"
                v-decorator="[
                  'status',
                  { valuePropName: 'checked', initialValue: formData.status == 1?true:false },
                ]"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <div class="submit">
          <a-button type="primary" @click="validate" :loading="loading2"
            >提交</a-button
          >
        </div>
      </a-card>
    </a-form>
  </div>
</template>

<script>
// import {
//   adminRoleList,
//   adminAdd,
//   adminAvatar,
// } from "@/services/admin";
// import { areaList, areaEdit } from "@/services/area";
import { areaEdit,areaOne } from "@/services/area";
export default {
  name: "AdminAdd",
  components: {},
  data() {
    return {
      form: this.$form.createForm(this),
      formData:{},
      loading:false,
      loading2: false,
      id:0,
      pid:0,
    };
  },
  created() {
    // console.log(this.$route.query.pid);
    this.id = this.$route.query.area_id

          //  areaSelOne('id')
    // this.title = this.$route.query.name
  },
  watch: {
    "$route.query.area_id": function () {
      // console.log(this.$route.query.area_id)
      let id = this.$route.query.area_id;
      if (id && this.id != id) {
         this.id = id
      }
      //重新调用请求数据的方法，刷新页面数据
    },
    async id(newValue, oldValue) {
      this.loading = true;
      let  areaSelOne = await  areaOne({id:newValue})
      if(areaSelOne.code == 200){
        // console.log(areaSelOne)
       this.pid =  areaSelOne.data.pid
       this.formData = areaSelOne.data
        //  this.form.setFieldsValue(areaSelOne.data);
      }
    },
  },
  methods: {
    async validate() {
      // alert()

      //  let
      // console.log();
      //   console.log(tree.checkedKeys);
      const { form, $message, $refs, $router } = this;

      this.form.validateFields((err) => {
        if (err) {
          this.$message.error("信息填写有误");
          return;
        } else {
          // console.log(form.getFieldsValue());
          let this_ = this
          this.$confirm({
            title: "确定要编辑吗?",
            async onOk() {
              this_.loading2 = true;
              let formData = form.getFieldsValue();
              let res = await areaEdit({formData:formData,conditions:{id:this_.id} });
              if (res.code == 200) {
                $message.success(res.message);
                this_.closeTabs.$emit("closeTabs");
                $router.push("/findShop/areaManagement");
               } 
            },
          });
        }
      });
    },
  },
};
</script>

<style scoped lang="less">
.aSelect {
  display: flex;
  width: 100%;
  justify-content: space-between;
  /* border: 1px solid red; */
  align-items: center;
}

.card {
  margin-bottom: 24px;
}

.form {
  .form-row {
    margin: 0 -8px;
    // display: flex;
    // justify-content: space-between;
    //    display: flex;
    // justify-content: space-between;
  }
  .ant-col-md-12,
  .ant-col-sm-24,
  .ant-col-lg-6,
  .ant-col-lg-8,
  .ant-col-lg-10,
  .ant-col-xl-8,
  .ant-col-xl-6 {
    padding: 0 8px;
  }
}
.topRow button {
  margin-top: 5px;
  margin-left: 10px;
}
.submit {
  width: 100%;
  display: flex;
  justify-content: center;
}
.portraitImage {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  //   border: 1px solid red;
}

#portraitImage {
  //   width: 100%;
  //   height: 100%;
}

.portraitShow {
  width: 70%;
  height: 0;
  padding-bottom: 70%;
  border: 1px dashed #ced0d3;
  // margin-left: 20px;
  position: relative;
  margin: auto;
  // display: flex;
  // justify-content: center;
  // align-items: center;
  cursor: pointer;
}
.plus-icon {
  position: absolute;
  left: 50%;
  top: 50%;
  font-size: 20px;
  transform: translate(-50%, -50%); /* 50%为自身尺寸的一半 */
}
</style>
